package com.example.imtoken.modules.main.my.address

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.OnMultifunctionalSelectorListener
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityAddressBinding
import com.example.imtoken.modules.main.my.address.create.CreateAddressActivity
import com.example.imtoken.view.function.MultifunctionalSelectorDialogFragment
import com.example.imtoken.view.function.data.onFunction1
import kotlinx.android.synthetic.main.include_head_view.*

class AddressActivity :
    BaseActivity<AddressViewModel, ActivityAddressBinding>(AddressViewModel::class.java) {

    private val isSelect by lazy {
        intent.getBooleanExtra("isSelect", false)
    }

    private lateinit var adapter: AddressAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_address
    }

    override fun init() {
        dataBinding.headView.tvHeadTitles.text = getString(R.string.addressBook)
        dataBinding.headView.ivHeadBack.setOnClickListener {
            this.finish()
        }
        dataBinding.headView.ivHeadFunction.setOnClickListener {
            val intent = Intent(this, CreateAddressActivity::class.java)
            startActivityForResult(intent, 0)
        }
        initView()
        initAdapter()
        initObserve()
        viewModel.getAddressBookData()
    }

    private fun initObserve() {
        viewModel.getAddressBook().observe(this) {
            adapter.setList(it)
        }
    }

    private fun initAdapter() {
        adapter = AddressAdapter(R.layout.item_address_book)
        val layoutManager = LinearLayoutManager(this)
        dataBinding.rvAddressBooks.layoutManager = layoutManager
        dataBinding.rvAddressBooks.adapter = adapter
        adapter.setEmptyView(R.layout.empty_view)
        adapter.setOnItemClickListener { _, _, p ->
            if (isSelect) {
                setResult(100, intent.putExtra("address", adapter.data[p].address))
                this.finish()
            } else {
                val dialog = MultifunctionalSelectorDialogFragment()
                dialog.setData(onFunction1(), object : OnMultifunctionalSelectorListener {
                    override fun selectPosition(position: Int) {
                        when (position) {
                            0 -> {
                                //复制地址
                                ClipboardUtils.copyText(adapter.data[p].address)
                                ToastUtils.showShort(R.string.copyTips)
                            }
                            1 -> {
                                //编辑
                                val intent =
                                    Intent(this@AddressActivity, CreateAddressActivity::class.java)
                                intent.putExtra("type", "edit")
                                intent.putExtra("id", adapter.data[p].id)
                                startActivityForResult(intent, 0)
                            }
                        }
                    }
                })
                dialog.show(supportFragmentManager, "dialog")
            }
        }
    }

    private fun initView() {
        ivHeadFunction.visibility = View.VISIBLE
        ivHeadFunction.setImageResource(R.drawable.ic_baseline_add_black_24)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            viewModel.getAddressBookData()
        }
    }

}